<template>
  <div class="changePwdForm">
    <el-row type="flex" justify="center" align="middle">
      <el-col :span="12">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-position="left"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="原密码" prop="oldpass">
            <el-input type="password" v-model="ruleForm.oldpass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "ChangePwd",
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入原密码"));
      } else {
        return callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入新密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // obj : getSessionStorage(),
      ruleForm: {
        oldpass: "",
        pass: "",
        checkPass: ""
      },
      rules: {
        oldpass: [{ validator: validateOldPass, trigger: "blur" }],
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  methods: {
    changepwd() {
      //   $.ajax({
      //     type: "post",
      //     url: "http://" + window.location.host + "/modifyPassword",
      //     data: {
      //       stuID: obj.stuID,
      //       pwd: $("#newpwd1").val()
      //     },
      //     dataType: "json",
      //     async: true,
      //     success: function(result) {
      //       if (result.msg != "failed") {
      //         sessionStorage.clear();
      //         alert("修改密码成功");
      //         // $(location).attr("href", "./login.html");
      //         this.$router.push("/login");
      //       } else {
      //         alert("修改密码失败！");
      //       }
      //     },
      //     error: function() {
      //       alert("err");
      //     }
      //   });
      axios
        .post("/modifyPassword", {
          stuID: obj.stuID,
          oldpwd: ruleForm.oldpass,
          pwd: ruleForm.pass
        })
        .then(function(response) {
          console.log(response);
          if (response.msg != "failed") {
            sessionStorage.clear();
            alert("修改密码成功");
            this.$router.push("/login");
          } else {
            alert("修改密码失败！");
          }
        })
        .catch(function(error) {
          console.log(error);
          alert("修改密码失败！");
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          alert("submit!");
          if(formName.n == '111'){
            console.log("111111111111111111111111");
          }else{
            console.log("2222222222222222222222222");
          this.$options.methods.changepwd();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.changePwdForm {
  margin: 20px auto 40px;
}
.el-row {
  margin-bottom: 20px;
}
.el-col {
  border-radius: 4px;
}
</style>